﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MS.INV
{
    public partial class Monitor : Form
    {
        public Monitor()
        {
            InitializeComponent();

            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.TopLevel = false;
        }

        private void navProduct_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            AddForm(new MainData.Catalogs());
        }

        private void AddForm(Form ff)
        {
            if (!CheckExists(ff.Name))
            {
                ff.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                ff.TopLevel = false;

                DevExpress.XtraTab.XtraTabPage tp = tabs.TabPages.Add();
                tp.Name = ff.Name;
                tp.Text = ff.Text;
                tp.Controls.Add(ff);
                ff.Dock = DockStyle.Fill;
                ff.Show();

                ff.FormClosing += new FormClosingEventHandler(ff_FormClosing);
                tabs.SelectedTabPage = tp;
            }
        }

        void ff_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                Form f = (Form)sender;
                DevExpress.XtraTab.XtraTabPage tp = GetTabPage(f.Name);
                if (tp != null)
                    //tp.Dispose();
                    tabs.TabPages.Remove(tp);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private DevExpress.XtraTab.XtraTabPage GetTabPage(string v_Name)
        {
            foreach (DevExpress.XtraTab.XtraTabPage tp in tabs.TabPages)
            {
                if (tp.Name.ToLower() == v_Name.ToLower())
                    return tp;
            }
            return null;
        }

        private bool CheckExists(string v_Name)
        {
            foreach (DevExpress.XtraTab.XtraTabPage tp in tabs.TabPages)
            {
                if (tp.Name.ToLower() == v_Name.ToLower())
                {
                    tabs.SelectedTabPage = tp;
                    return true;
                }
            }

            return false;
        }

        private void NavSupplier_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            AddForm(new MainData.Customers());
        }

        private void NavTable_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            AddForm(new MainData.Tables());
        }

        private void tabs_CloseButtonClick(object sender, EventArgs e)
        {
            DevExpress.XtraTab.XtraTabPage tp = tabs.SelectedTabPage;
            Form f = (Form)tp.Controls[0];
            f.Close();
        }

        private void NavPriceList_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            AddForm(new Prices.PriceList());
        }

        private void NavFormula_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            AddForm(new Prices.PrimeCost());
        }

        private void navRateUnit_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            AddForm(new MainData.UnitConversion());
        }

        private void navUnit_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            AddForm(new Inventory.PhieuNhap());
        }

        private void navViTri_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            AddForm(new Inventory.PhieuXuat());
        }

        private void navNhom_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            AddForm(new MixedDrink.PhaChe());
        }

        private void mnPC_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            AddForm(new MixedDrink.Pc_DinhMuc());
        }

        private void navTonDau_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            AddForm(new MainData.Kho_TonDau());
        }

        private void navCost_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            AddForm(new Costs.ChiPhi());
        }

        private void navKiemKe_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            AddForm(new Inventory.KiemKeKho());
        }
    }
}
